var RunLayer202 = cc.Layer.extend({
	arr:null,
	flow:null,
	scene:null,
	clock:null,
	ctor:function (parent) {
		this._super();
		this.scene = parent;
		this.scene.addChild(this, 10);
		this.init();
	},
	init:function () {
		this.clock = new Clock(this,"#other/clock.png");
		
		var kitchenItem = new Button(this,
				d_z_index, TAG_ITEM_KITCHEN,
				"#kitchen.png",
				this.eventMenuCallback,this);
		kitchenItem.setPosition(gg.width * 0.01 + kitchenItem.width * 0.5, gg.height * 0.45);
		
		var pipeItem = new Button(this,
				5, TAG_ITEM_PIPE,
				"#pipe.png",
				this.eventMenuCallback,this);
		pipeItem.setPosition(270, 280);
		
		var deskItem = new Button(this,
				d_z_index, TAG_ITEM_DESK,
				"#desk.png",
				this.eventMenuCallback,this);
		deskItem.setPosition(gg.width * 0.01 + kitchenItem.width + 20 + deskItem.width * 0.5, gg.height * 0.25);
		var deskY = deskItem.y + deskItem.height * 0.5 - 40;
		this.deskY = deskY;

		var headItem = new Button(this,
				10, TAG_ITEM_HEAD,
				"#coolpex_head.png",
				this.eventMenuCallback,this);
		headItem.setPosition(390, 428);

		this.bodySprite = new CoolpexBody(this,
				10, TAG_ITEM_BODY, "#coolpex_body.png",
				this.eventMenuCallback,this);
		this.bodySprite.setPosition(390, 325);
		
		var tankItem = new Button(this,
				d_z_index, TAG_ITEM_TANK,
				"#tank.png",
				this.eventMenuCallback,this);
		tankItem.setPosition(0.59 * gg.width, deskY + tankItem.width * 0.5 - 10);
		// 控制罐
		var ctrlItem =  new Button(this,
				11, TAG_ITEM_TANK_CTRL,
				"#tank/kuangjia5.png",
				this.eventMenuCallback,this);
		ctrlItem.setPosition(0.56 * gg.width+11, deskY + tankItem.width * 0.5 + 9);
		ctrlItem.setVisible(false);
		ctrlItem.setScale(0.4);
		
		// 控制罐0.2倍
		var ctrlItem2 =new Button(this,
				11, TAG_ITEM_TANK_CTRL2,
				"#tank/ctrl.png",
				this.eventMenuCallback,this);
		ctrlItem2.setPosition(cc.p(179,317));
		ctrlItem2.setVisible(false);

		// 标准罐
		var standItem =  new Button(this,
				11, TAG_ITEM_TANK_STAND,
				"#tank/kuangjia4.png",
				this.eventMenuCallback,this);
		standItem.setPosition(0.57 * gg.width + standItem.width * 0.5+15, deskY + tankItem.width * 0.5 + 9);
		standItem.setVisible(false);
		standItem.setScale(0.4);
		
		// 标准罐0.2倍
		var standItem2 =new Button(this,
				11, TAG_ITEM_TANK_STAND2,
				"#tank/stand.png",
				this.eventMenuCallback,this);
		standItem2.setPosition(cc.p(149,317));
		standItem2.setVisible(false);

		
		var saidItem = new Button(this,
				10, TAG_ITEM_SAID,
				"#said.png",
				this.eventMenuCallback,this);
		saidItem.setPosition(gg.width * 0.73, deskY + this.bodySprite.width * 0.5 - 10);
		
		// DKQ
		var dkqItem = new Button(this,
				10, TAG_ITEM_DKQ,
				"#dkq1.png",
				this.eventMenuCallback,this);
		dkqItem.setPosition(100, 305);
		
		var conical = new Conical(this);
		conical.setPosition(830, 310);
		
		var socket = new Button(this,
				-2, TAG_ITEM_SOCKET,
				"#socket/socket.png",
				this.eventMenuCallback,this);
		socket.setPosition(550, 473);
		
		this.plug = new cc.Sprite("#socket/plug1.png");
		this.plug.setPosition(480, deskY + this.plug.width * 0.5 + 30);
		this.addChild(this.plug, -1, TAG_ITEM_PLUG);
		
		this.useUp();
		this.initAction();
		
		this.loadHead();
		this.loadTank();
		this.loadSide();
		this.loadSaid();
		this.loadDKQ();
	},
	initAction:function() {
		var animFrames = [];
		for (var i = 1; i < 6; i++) {
			var str = "socket/plug" + i + ".png";
			var frame = cc.spriteFrameCache.getSpriteFrame(str);
			animFrames.push(frame);
		}
		var animation = new cc.Animation(animFrames, 0.12);
		this.plugAction = cc.repeat(cc.Animate.create(animation), 1);
		this.plugAction.retain();// jsb BUG
	},
	loadHead:function(){
		if(this.head == null){
			this.head = new Head(this, gg.flow, TAG_COOLPEX_HEAD);	
		}
	},
	loadTank:function(){
		if(this.tank == null){
			this.tank = new Tank(this, gg.flow, TAG_COOLPEX_TANK);
		}
	},
	loadSide:function(){
		if(this.side == null){
			this.side = new Side(this, gg.flow, TAG_COOLPEX_SIDE);
		}
	},
	loadSaid:function(){
		if(this.said == null){
			this.said = new Said(this, gg.flow, TAG_COOLPEX_SAID);			
		}
	},
	loadDKQ:function(){
		if(this.dkq == null){
			this.dkq = new Dkq(this, gg.flow, TAG_COOLPEX_DKQ);
			gg.dkq = this.dkq;
		}
	},
	useUp:function() {
		var margin = 5, fontSize = 20;
		
		var paperItem = new Button(this,
				10, TAG_ITEM_PAPER,
				"#other/paper_item.png",
				this.eventMenuCallback,this);
		paperItem.setPosition(gg.width * 0.6, gg.height - paperItem.width * 0.5 - 10);
		
		this.label1 = new Label(this, "称量纸");
		this.label1.down(paperItem, margin);
		this.label1.setFontSize(fontSize);
		
		var spoonItem = new Button(this,
				10, TAG_ITEM_SPOON,
				"#other/spoon_item.png",
				this.eventMenuCallback,this);
		spoonItem.right(paperItem);
		
		this.label2 = new Label(this, "药匙");
		this.label2.down(spoonItem, margin);
		this.label2.setFontSize(fontSize);
		
		var pipetteItem = new Button(this,
				10, TAG_ITEM_PIPETTE,
				"#other/pipette_item.png",
				this.eventMenuCallback,this);
		pipetteItem.right(spoonItem);
		
		this.label3 = new Label(this, "移液管");
		this.label3.down(pipetteItem, margin);
		this.label3.setFontSize(fontSize);
		
		// 扳手
		var banshouItem = new Button(this,
				101, TAG_TANK_BANSHOU,
				"#other/banshou_item.png",
				this.eventMenuCallback,this);
		banshouItem.right(pipetteItem);
		banshouItem.orig_p = banshouItem.getPosition();
		
		var label4 = new Label(this, "扳手");
		label4.down(banshouItem, margin);
		label4.setFontSize(fontSize);
	},
	pinTank:function(){
		var tank = this.getChildByTag(TAG_ITEM_TANK);
		tank.setVisible(false);
		var ctrl = this.getChildByTag(TAG_ITEM_TANK_CTRL);
		var stand = this.getChildByTag(TAG_ITEM_TANK_STAND);
		ctrl.setVisible(true);
		stand.setVisible(true);
	},
	showDesc:function(tag){
		if(gg.cur_tool == TAG_BUTTON_HELP){
			for(var i = 0; i< sprite_json.length; i++){
				if(sprite_json[i].tag == tag){
					this.scene.tip_layer.sayFlash(sprite_json[i].desc);
					cc.log(sprite_json[i].desc);
					break;
				}
			}
		}
	},
	eventMenuCallback:function (pSender){
		cc.log("say:" + pSender.getTag());
		var action = gg.flow.flow.action;
		var func = cc.callFunc(this.checkActionDone, this);
		gg.synch_listener = false;
		switch (pSender.getTag()){
			case TAG_ITEM_HEAD:
				gg.flow.next();
				this.head.open();
				if(action==ACTION_DO1){
					this.head.string = "1--0520W--"+this.head.curDegree+"°C--0.0atm--03:00\n";
					this.head.string += "0000W  017°C--00.0atm  00.00\n";
					this.head.string += "状态:准备    0017 00.0    00:00\n";
					this.head.string += "----------------------------------\n";
					this.head.string += "   °C                         atm\n";
					this.head.string += "150-|                           |-30\n";
					this.head.string += "100-|                           |-20\n";
					this.head.string += "050-|                           |-10\n";
					this.head.string += "000-|-----------------------------00\n";
					this.head.string += "    0 3 6 9 12 15 18 21 24 27 30";
					this.head.curDegree --;
					this.head.say();
				}
				break;
			case TAG_ITEM_BODY:
				if(action == ACTION_DO1){
					// 进入侧面
					gg.flow.next();
					this.side.open();
				} else if(action == ACTION_NONE){
					// 观察炉腔内 && 拧松导气管接头
					gg.flow.next();
				} else if(action == ACTION_DO2){
					// 关上炉门
					this.bodySprite.closeDoor2();
					var stand = this.getChildByTag(TAG_ITEM_TANK_STAND);
					stand.runAction(cc.fadeOut(0.5));
					gg.flow.next();
				} else if(action == ACTION_DO4){
					// 关上安全门
					this.bodySprite.closeDoor();
					var ctrl = this.getChildByTag(TAG_ITEM_TANK_CTRL);
					ctrl.runAction(cc.fadeOut(0.5));
					gg.flow.next();
					// 等待气压为0
					this.clock.doTime(4, 0);
					this.clock.loadTip("#tool/tip3.png");
				}
				break;
			case TAG_ITEM_TANK:
				gg.flow.next();
				this.tank.open();
				break;
			case TAG_ITEM_SOCKET:
				var seq = cc.sequence(this.plugAction,cc.callFunc(this.checkActionDone, this));
				this.plug.runAction(seq);
				break;
			case TAG_ITEM_PIPE:
				gg.flow.next();
				break;
			case TAG_ITEM_SAID:
				this.said.open();
				var cur_flow = gg.teachFlow[gg.flow.step - 1];
				if(cur_flow.step_zero){
					this.said.step = 1;
					this.said.say("0.1000g");
				}
				gg.flow.next();
				break;
			case TAG_ITEM_PAPER:
				if(action == ACTION_DO1){
					pSender.setSpriteFrame("other/paper.png");
					pSender.move(func, cc.p(736,305), 0.8);
					this.label1.setVisible(false);
				} else if(action == ACTION_DO2){
					var totate = cc.rotateTo(0.5, -45);
					pSender.move2(func, cc.p(620,345),totate, 0.8);
				}
				break;
			case TAG_ITEM_SPOON:
				pSender.setSpriteFrame("other/spoon.png");
				var rotate = cc.rotateTo(0.4, -60);
				var move = cc.moveTo(0.5, cc.p(870, 400));
				var move2 = cc.moveTo(0.5, cc.p(835, 330));
				var ff1 = cc.callFunc(function(){
					var conical = this.getChildByTag(TAG_CONICAL);
					conical.rotateBody1();
				}, this);
				var delay = cc.delayTime(0.3);
				var move3 = cc.moveTo(0.5, cc.p(870, 400));
				var ff2 = cc.callFunc(function(){
					var conical = this.getChildByTag(TAG_CONICAL);
					conical.rotateBody2();
				}, this);
				var rotate2 = cc.rotateTo(0.4, -40);
				var move4 = cc.moveTo(0.5, cc.p(760, 340));
				var seq = cc.sequence(move, ff1, rotate, move2, delay, move3, ff2, rotate2, move4, func);
				pSender.runAction(seq);
				break;
			case TAG_ITEM_PIPETTE:
				pSender.setSpriteFrame("other/pipette.png");
				var totate = cc.rotateTo(0.8, 30);
				pSender.move2(func, cc.p(118, 400),totate, 1.8);
				break;
			case TAG_ITEM_TANK_CTRL:
				if(action == ACTION_DO1){
					pSender.move(func, cc.p(413, 330), 0.6);
				} else if(action == ACTION_DO2){
					gg.flow.next();
					this.tank.open();
				}else if(action == ACTION_DO5){
					// 取出控制罐
					var ctrl = this.getChildByTag(TAG_ITEM_TANK_CTRL);
					var ctrl2 = this.getChildByTag(TAG_ITEM_TANK_CTRL2);
					var move1 = cc.moveTo(0.6,cc.p(180,315));
					var scale1 = cc.scaleTo(0.6, 0.2);
					var spawn = cc.spawn(move1, scale1);
					var seq = cc.sequence(spawn,func);
					ctrl.runAction(seq);	
				}
				break;
			case TAG_ITEM_TANK_CTRL2:
				if(action==ACTION_DO2){
					gg.flow.next();
					this.tank.open();
				}
				break;
			case TAG_ITEM_TANK_STAND:
				if(action == ACTION_DO1){
					pSender.move(func, cc.p(413, 330), 0.6);
				} else if(action == ACTION_DO2){
					gg.flow.next();
				}else if(action == ACTION_DO3){
					// 取出消解罐
					var stand = this.getChildByTag(TAG_ITEM_TANK_STAND);
					var stand2 = this.getChildByTag(TAG_ITEM_TANK_STAND2);
					var move1 = cc.moveTo(0.6,cc.p(150,315));
					var scale1 = cc.scaleTo(0.6, 0.2);
					var spawn = cc.spawn(move1, scale1);
					var seq = cc.sequence(spawn,func);
					stand.runAction(seq);		
				}
				break;
			case TAG_ITEM_TANK_STAND2:
				if(action==ACTION_DO2){
					gg.flow.next();
				}
				break;
			case TAG_TANK_BANSHOU:
				if(action == ACTION_DO1){
					pSender.move(func, cc.p(300,410), 0.6);
					pSender.setSpriteFrame("other/banshou.png");;					
					this.tank.step ++;
				} else if(action == ACTION_DO2){
					_.ka();
					var totate = cc.rotateTo(0.6, 0);
					pSender.move3(func, pSender.orig_p, totate, 0.6);
					// 桌面上消解罐拼装完成
					this.pinTank();
					this.tank.step ++;
				} else if(action == ACTION_DO3){
					var ban5= this.tank.byTag(TAG_BANSHOU5);
					pSender.setSpriteFrame("other/banshou.png");
					var move1 = cc.moveTo(0.6, cc.p(300,400));
					var move2 = cc.moveTo(0.6, pSender.orig_p);
					var del=cc.delayTime(0.2);
					var seq=cc.sequence(move1,cc.callFunc(function() {
						pSender.setSpriteFrame("other/banshou4.png");
					}, this),del,cc.callFunc(function() {
						ban5.setVisible(true);
						pSender.setVisible(false);
					}, this),del,cc.callFunc(function() {
						ban5.setVisible(false);
						pSender.setVisible(true);
					}, this),del,cc.callFunc(function() {
						ban5.setVisible(true);
						pSender.setVisible(false);
					}, this),del,cc.callFunc(function() {
						ban5.setVisible(false);
						pSender.setVisible(true);
					}, this),move2,cc.callFunc(function() {
						pSender.setSpriteFrame("other/banshou_item.png");
					}, this),func);
					pSender.runAction(seq);
				}
				break;
			case TAG_ITEM_DKQ:
				gg.flow.next();
				this.dkq.open();
				break;		
		}
	},
	checkActionDone:function(pSender){
		var tag = pSender.getTag();
		var action = gg.flow.flow.action;
		var stand2 = this.getChildByTag(TAG_ITEM_TANK_STAND2);
		var ctrl2 = this.getChildByTag(TAG_ITEM_TANK_CTRL2);
// var banshou2=this.tank.byTag(TAG_TANK_BANSHOU2);
		if(tag == TAG_TANK_BANSHOU){
			if(action == ACTION_DO1){
				pSender.setVisible(false);
				var ban4=this.tank.byTag(TAG_BANSHOU4);
				ban4.setVisible(true);			
			}else if(action == ACTION_DO2){
				pSender.setSpriteFrame("other/banshou_item.png");
			} else if(action == ACTION_DO3){
				pSender.setSpriteFrame("other/banshou_item.png");
			}
		}else if(tag == TAG_ITEM_PAPER){
			if(action == ACTION_DO1){
// pSender.setOpacity(200);
			} else if (action == ACTION_DO2) {
				pSender.setVisible(false);
			}
		} else if(tag == TAG_ITEM_SPOON){
			pSender.setVisible(false);
			this.label2.setVisible(false);
			var paper = this.getChildByTag(TAG_ITEM_PAPER);
			paper.setSpriteFrame("other/paper2.png");
		} else if(tag == TAG_ITEM_PIPETTE){
			pSender.setVisible(false);
			this.label3.setVisible(false);
		}else if(tag==TAG_ITEM_TANK_STAND){
			if(action==ACTION_DO3){
				stand2.setVisible(true);
			}
		}else if(tag==TAG_ITEM_TANK_CTRL){
			if(action==ACTION_DO5){
				ctrl2.setVisible(true);
			}
		}
		gg.flow.next();
	},
	over: function (){
		ll.tip.over();
		this.scheduleOnce(function(){
			gg.synch_l = false;
			ch.gotoFinish(TAG_EXP_02, g_resources_finish02, new FinishScene02());
		},2);
		// 提交成绩
		net.saveScore();
	}
});